/*
05-树9 Huffman Codes
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

/* strct =====================================*/
// Huffman 编码长度
#ifndef HuffmanCodeLength
#define HuffmanCodeLength 50
#endif

// Huffman 树节点
typedef struct HuffmanNode HuffmanNode;
struct HuffmanNode {
    //结点值
    char data[HuffmanCodeLength/5]; 
    char code[HuffmanCodeLength];
    // 权重 
    double weight;
};

// 树
#define TreeElementType HuffmanNode
typedef struct TreeNode *Tree;
typedef struct TreeNode TreeNode;
struct TreeNode {
    TreeElementType data;
    TreeNode *left;
    TreeNode *right;
};

// 堆
// 使用堆来构建 Huffman，堆元素类型为树节点指针 TreeNode*
#define HeapElementType TreeNode*
typedef struct Heap Heap;
struct Heap {
    HeapElementType* data; // 存储元素的数组
    int size; // 元素个数
    int capacity; // 最大容量
    // 堆元素比较器
    int (*compare)(HeapElementType e1, HeapElementType e2);
};

/* Tree =======================================*/
TreeNode* createTreeNode(TreeElementType data) {
    TreeNode* node = calloc(1, sizeof(TreeNode));
    node->data = data;
    return node;
}

void freeTree(TreeNode* root) {
    if(root->left) {
        freeTree(root->left);
    }
    if(root->right) {
        freeTree(root->right);
    }
    free(root);
}

// 访问树节点时需要完成的操作，一般是输出，
// 这里定义的是函数头，在使用的程序里写具体的实现
void visitTreeNode(TreeElementType e);

void preorderTraversal(Tree root) {
    if (root == NULL) {
        return;
    }
    visitTreeNode(root->data);
    preorderTraversal(root->left);
    preorderTraversal(root->right);
}

/* Heap =======================================*/
Heap* createHeap(int capacity, int (*compare)(HeapElementType e1, HeapElementType e2)) {
    Heap* h = calloc(1, sizeof(Heap));
    h->data = calloc((capacity+1), sizeof(HeapElementType));
    h->size = 0;
    h->capacity = capacity;
    // h->data[0] = -32767; // 本来应该定义“哨兵”，但因使用比较器，“哨兵”不好确定
    h->compare = compare;
    return h;
}

void freeHeap(Heap* h) {
    free(h->data);
    free(h);
}

int isFullHeap(Heap* h) {
    return h->size+1 > h->capacity;
}

int isEmptyHeap(Heap* h) {
    return h->size < 1;
}

void insertHeap(Heap* h, HeapElementType item) {
    if (isFullHeap(h)) {
        printf("Heap Full");
        return;
    }
    int i = ++h->size;
    // 调整:比较item，与item的父节点（下标：i/2），若大于父节点，把父节点往下移动
    for (; i/2>0 && h->compare(item, h->data[i/2]) > 0; i/=2) {
        h->data[i] = h->data[i/2];
    }
    h->data[i] = item;
}

HeapElementType deleteHeap(Heap *h) {
    if (isEmptyHeap(h)) {
        printf("Heap Empty\n");
        return 0;
    }
    // 取出最大的节点
    HeapElementType minItem = h->data[1];
    // 先把最后一个节点放入第一个节点位置
    HeapElementType temp = h->data[h->size--];
    // 再调整堆
    int parent, child;
    // 为temp找一个位置
    // parent*2 < h->size 判断是否有左儿子
    for (parent = 1; parent*2 <= h->size; parent = child) {
        child = parent * 2;
        if ( (child != h->size) && h->compare(h->data[child], h->data[child+1]) < 0) {
            // child != h->size 是否有右儿子
            // 找到左右节点中更大的节点
            child++;
        }
        // 根已经大于等于孩子节点，结束循环
        if (h->compare(temp, h->data[child]) >= 0) {
            break;
        }
        // 把更大的子节点复制到父节点位置
        h->data[parent] = h->data[child];
    }
    h->data[parent] = temp;
    return minItem;
}

/* Huffman =============================================*/
HuffmanNode createHuffmanNode(char* data, double weight) {
    HuffmanNode node;
    if (data == NULL) {
        node.data[0] = 0;
    } else {
        strcpy(node.data, data);
    }
    node.code[0] = 0;
    node.weight = weight;
    return node;
}

TreeNode* createHuffmanFromMinHeap(Heap* minHeap) {
    for(int i=1, len = minHeap->size; i < len; i++) {
        Tree left = deleteHeap(minHeap);
        Tree right = deleteHeap(minHeap);
        HuffmanNode hfNode = createHuffmanNode(NULL, left->data.weight + right->data.weight);
        TreeNode* node = createTreeNode(hfNode);
        node->left = left;
        node->right = right;
        insertHeap(minHeap, node);
    }
    return deleteHeap(minHeap);
}

//计算最优编码长度Weighted Path Length
int calHuffmanWPL(Tree hf, int depth){  // Depth从0开始
    if(hf->left == NULL && hf->right == NULL)
        return (depth * hf->data.weight);
    else{ // 有左右孩子,WPL= 左WPL+右WPL
        return calHuffmanWPL(hf->left, depth+1) + calHuffmanWPL(hf->right, depth+1);
    }
}

// 计算 Huffman 编码
void calHuffmanCode(Tree hf) {
    if (hf->left != NULL) {
        strcpy(hf->left->data.code, hf->data.code);
        strcat(hf->left->data.code, "0");
        calHuffmanCode(hf->left);
    } 
    if (hf->right != NULL) {
        strcpy(hf->right->data.code, hf->data.code);
        strcat(hf->right->data.code, "1");
        calHuffmanCode(hf->right);
    }
}

// 输出 Huffman 编码
void visitTreeNode(TreeElementType e) {
    if (strlen(e.data) > 0) {
        printf("%s(%3.1f): %s\n", e.data, e.weight, e.code);
    }
}

/* crrent =========================================*/
#define MaxSize 65

// 比较两个节点，负数 - e1 < e2; 0 - e1 == e2; 正数 - e1 > e2
int compareHeapNode(HeapElementType e1, HeapElementType e2) {
    if (e2->data.weight > e1->data.weight) {
        return 1;
    } else if (e2->data.weight < e1->data.weight) {
        return -1;
    } else {
        return 0;
    }
}

// 检查前缀
int checkPrefix(char code[][MaxSize], int n){
    int length;
    for(int i=0; i<n; i++){
        for (int j=i+1; j<n; j++){
            length = strlen(code[i]) < strlen(code[j]) ? strlen(code[i]) : strlen(code[j]);
            if (strncmp(code[i], code[j], length) == 0)
                return 0;
        }
    }
    return 1;
}

int check(int n, int wpl, int* f, char code[][MaxSize]) {
    // 检查wpl是否正确
    int s_wpl = 0;
    for (int i = 0; i<n; i++){
        s_wpl += strlen(code[i]) * f[i];
    }
    if (wpl != s_wpl) {
        return 0;
    }
    return checkPrefix(code, n);
}

int main() {
    int n, f[MaxSize];
    scanf("%d", &n);
    Heap* minHeap = createHeap(n, compareHeapNode);
    for (int i=0; i<n; i++) {
        char c[10];
        scanf("%s %d", c, &f[i]);
        // printf("c=%s, f=%d\n", c, f);
        HuffmanNode hfNode = createHuffmanNode(c, f[i]);
        HeapElementType node = createTreeNode(hfNode);
        insertHeap(minHeap, node);
    }
    Tree huffman = createHuffmanFromMinHeap(minHeap);
    freeHeap(minHeap);
    // printHuffman(huffman);
    // printf("\n");
    int wpl = calHuffmanWPL(huffman, 0);
    // printf("wpl = %d\n", wpl);
    // calHuffmanCode(huffman);
    // preorderTraversal(huffman);
    freeTree(huffman);

    int m;
    scanf("%d\n", &m);
    // printf("m=%d\n", m);
    for (int j=0; j<m; j++) {
        char code[MaxSize][MaxSize];
        for (int i=0; i<n; i++) {
            char c[4];
            scanf("%s %s", c, code[i]);
            // printf("c = %s, code = %s\n", c, code[i]);
        }
        if (check(n, wpl, f, code)) {
            printf("Yes\n");
        } else {
            printf("No\n");
        }
    }
    return 0;
}
